package cn.pli.bike.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.pli.bike.R;


/**
 * Created by pc on 2016/4/29.
 */
public class DialogUtils {

    public Dialog showDialog(Context context, View.OnClickListener listener, int layputId){
        final Dialog dialog = new Dialog(context, R.style.translucent_dialog);
        dialog.setCancelable(false);//使双按钮对话框点击屏幕不可被取消
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
                }
                return true;
            }
        });
        dialog.setContentView(layputId);
        return dialog;
    }
   /* 从下而上弹出对话框
    *
            * @param ctx
    * @param contentview
    *            对话框的布局
    */
    public static Dialog getDownToUpDialog(Activity act, int contentview) {
        Dialog dialog = new Dialog(act, R.style.loginDialog);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(contentview);
        // 动画弹出
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.dimAmount = 0.8f;
        lp.width = act.getWindowManager().getDefaultDisplay().getWidth();
        window.setWindowAnimations(R.style.foxyDialogStyle);
        return dialog;
    }

    public static Dialog getNormalDialog(Activity act, int contentview) {
        Dialog dialog = new Dialog(act, R.style.loginDialog);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(contentview);
        // 宽度、透明度设置
        Window window = dialog.getWindow();
        LayoutParams lp = window.getAttributes();
        lp.dimAmount = 0.8f;
        lp.width = act.getWindowManager().getDefaultDisplay().getWidth() - UnitConverterUtil.dp2px(act, 20);
        return dialog;
    }

    public static Dialog getDialog(Context ctx, View contentview) {
        Dialog dialog = new Dialog(ctx, R.style.loginDialog);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(contentview);
        return dialog;
    }

    public static Dialog getDialog(Activity act, int contentview) {
        Dialog dialog = new Dialog(act, R.style.loginDialog);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(contentview);
        // 宽度、透明度设置
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.dimAmount = 0.4f;
        lp.width = act.getWindowManager().getDefaultDisplay().getWidth() - UnitConverterUtil.dp2px(act, 5);
        return dialog;
    }

    public static Dialog getADialog(Activity act, int contentview) {
        Dialog dialog = new Dialog(act, R.style.loginDialog);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(contentview);
        // 宽度、透明度设置
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.dimAmount = 0.4f;
        lp.width = act.getWindowManager().getDefaultDisplay().getWidth() - UnitConverterUtil.dp2px(act, 50);
        return dialog;
    }

    public static Dialog getCreateDialog(Context ctx, String setText) {
        // 加载样式
        Dialog dialog = new Dialog(ctx, R.style.dialog);
        View v = LayoutInflater.from(ctx).inflate(R.layout.activity_dialog, null);// 得到加载view
        TextView tv = (TextView) v.findViewById(R.id.tv_dialog);
        tv.setText(setText);
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.alpha = 0.8f;
        dialog.getWindow().setAttributes(lp);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(v);
        return dialog;
    }





}
